//
//  BSSBaseNavigationBar.m
//  BSSBaseDependTool
//
//  Created by wanglei on 2019/4/15.
//

#import "BSSBaseNavigationBar.h"

#define BSSNavCenterY ([UIApplication sharedApplication].statusBarFrame.size.height + 44/2)
#define BSSButtonSpace 10

@interface BSSBaseNavigationBar()

@end
@implementation BSSBaseNavigationBar

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createUI];
        [self layoutUI];
    }
    return self;
}

- (void)createUI{
    [self addSubview:self.backgroundView];
    [_backgroundView addSubview:self.statusBarView];
    [_backgroundView addSubview:self.navigationView];
    [_navigationView addSubview:self.titleLabel];
    [_navigationView addSubview:self.leftButton];
    [_navigationView addSubview:self.rightButton];
    [self addSubview:self.separator];
}

- (void)layoutUI{
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    [_statusBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_offset(__kStatusBarHeight__);
    }];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.top.mas_equalTo(_statusBarView.mas_bottom);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(_navigationView);
        make.left.mas_equalTo(44);
        make.width.mas_lessThanOrEqualTo(__kScreenWidth__/2.0);
    }];
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(_navigationView);
        make.width.mas_greaterThanOrEqualTo(44);
    }];
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(_navigationView);
        make.width.mas_greaterThanOrEqualTo(44);
    }];
    [_separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}
#pragma mark - 自定义操作
+ (BSSBaseNavigationBar *)initWithBackgroundView:(UIView *)backgroundView
                                        toNavBar:(BSSBaseNavigationBar *)navBar{
    navBar.titleLabelType = BSSNavBarTitleLabelTypeNone;
    navBar.leftButtonType = BSSNavBarLeftButtonTypeNone;
    navBar.rightButtonType = BSSNavBarRightButtonTypeNone;
    
    [navBar.backgroundView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(navBar.backgroundView);
    }];
    
    return navBar;
}

+ (BSSBaseNavigationBar *)initWithNavView:(UIView *)navigationView
                                 toNavBar:(BSSBaseNavigationBar *)navBar{
    navBar.titleLabelType = BSSNavBarTitleLabelTypeNone;
    navBar.leftButtonType = BSSNavBarLeftButtonTypeNone;
    navBar.rightButtonType = BSSNavBarRightButtonTypeNone;
    
    [navBar.navigationView addSubview:navigationView];
    [navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(navBar.navigationView);
    }];
    
    return navBar;
}


#pragma mark - set TYPE
- (void)setNavBartype:(BSSNavBarType)navBartype{
    _navBartype = navBartype;
    [self clearBackground];
    if (navBartype == BSSNavBarTypeClear) {
        self.backgroundColor = [UIColor clearColor];
        self.navigationView.image = nil;
        self.navigationView.hidden = YES;
        self.separator.hidden = YES;
    } else if(navBartype == BSSNavBarTypeLight) {
        self.backgroundColor = [UIColor whiteColor];
        self.navigationView.hidden = NO;
        self.separator.hidden = YES;
    }
    [self setTitleLabelType:self.titleLabelType];
    [self setLeftButtonType:self.leftButtonType];
    [self setRightButtonType:self.rightButtonType];
}
- (void)setTitleLabelType:(BSSNavBarTitleLabelType)titleLabelType{
    _titleLabelType = titleLabelType;
    
    if (titleLabelType == BSSNavBarTitleLabelTypeLeft) {
        _titleLabel.textColor = __kColor(21, 79, 21);
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        
        _titleLabel.hidden = NO;
    } else if (titleLabelType == BSSNavBarTitleLabelTypeCenter) {
        _titleLabel.textColor = __kColor(21, 79, 21);
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
        _titleLabel.hidden = NO;
    } else if (titleLabelType == BSSNavBarTitleLabelTypeNone) {
        _titleLabel.hidden = YES;
    }
    
}
- (void)setLeftButtonType:(BSSNavBarLeftButtonType)leftButtonType{
    _leftButtonType = leftButtonType;
    [self clearLeftButton];
    if (leftButtonType == BSSNavBarLeftButtonTypeBack) {
        [self.leftButton setImage:imageNameStr(@"ic_left_blod") forState:UIControlStateNormal];
        
        self.leftButton.hidden = NO;
        self.leftButton.userInteractionEnabled = YES;
        
    } else if(leftButtonType == BSSNavBarLeftButtonTypeNone){
        self.leftButton.hidden = YES;
    }
}
- (void)setRightButtonType:(BSSNavBarRightButtonType)rightButtonType{
    _rightButtonType = rightButtonType;
    [self clearRightButton];
    if (rightButtonType == BSSNavBarRightButtonTypeNone) {
        self.rightButton.hidden = YES;
    } else if(rightButtonType == BSSNavBarRightButtonTypeShare){
        self.rightButton.hidden = NO;
        [self.rightButton setImage:[UIImage ff_imagePathWithName:@"nav_share" bundle:@"BSSBaseDependBundle" targetClass:[BSSBaseNavigationBar class]] forState:UIControlStateNormal];
    }
}

#pragma mark - 清除

- (void)clearLeftButton{
    [self.leftButton setTitle:nil forState:UIControlStateNormal];
    [self.leftButton setTitleColor:nil forState:UIControlStateNormal];
    [self.leftButton setImage:nil forState:UIControlStateNormal];
    [self.leftButton setBackgroundImage:nil forState:UIControlStateNormal];
    self.leftButton.hidden = YES;
}

- (void)clearRightButton{
    [self.rightButton setTitle:nil forState:UIControlStateNormal];
    [self.rightButton setTitleColor:nil forState:UIControlStateNormal];
    [self.rightButton setImage:nil forState:UIControlStateNormal];
    [self.rightButton setBackgroundImage:nil forState:UIControlStateNormal];
    self.rightButton.hidden = YES;
}

- (void)clearBackground{
    self.backgroundView.backgroundColor = [UIColor clearColor];
    self.backgroundView.image = nil;
}


#pragma mark - 按钮事件
- (void)leftButtonClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(bss_navBarLeftButtonClicked:)]) {
        [self.delegate bss_navBarLeftButtonClicked:sender];
    }
}
- (void)rightButtonClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(bss_navBarRightButtonClicked:)]) {
        [self.delegate bss_navBarRightButtonClicked:sender];
    }
}
#pragma mark - setter&&getter
- (NSString *)title {
    return [self.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (UIImageView *)backgroundView{
    if (!_backgroundView) {
        _backgroundView = [UIImageView new];
        _backgroundView.backgroundColor = [UIColor clearColor];
        _backgroundView.userInteractionEnabled = YES;
        _backgroundView.contentMode = UIViewContentModeScaleToFill;
    }
    return _backgroundView;
}
- (UIImageView *)statusBarView{
    if (!_statusBarView) {
        _statusBarView = [UIImageView new];
        _statusBarView.backgroundColor = [UIColor clearColor];
        _statusBarView.userInteractionEnabled = YES;
        _statusBarView.contentMode = UIViewContentModeScaleToFill;
        
        _statusBarView.image = [UIImage ff_imagePathWithName:@"threestateback" bundle:@"BSSBaseDependBundle" targetClass:[BSSBaseNavigationBar class]];
    }
    return _statusBarView;
}
- (UIImageView *)navigationView{
    if (!_navigationView) {
        _navigationView = [UIImageView new];
        _navigationView.backgroundColor = [UIColor clearColor];
        _navigationView.userInteractionEnabled = YES;
        _navigationView.contentMode = UIViewContentModeScaleToFill;
    }
    return _navigationView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.backgroundColor = [UIColor clearColor];
    }
    return _titleLabel;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _leftButton.hidden = YES;
    }
    return _leftButton;
}
- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _rightButton.hidden = YES;
    }
    return _rightButton;
}
- (UIView *)separator{
    if (!_separator) {
        _separator = [UIView new];
        _separator.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
        
        _separator.hidden = YES;
    }
    return _separator;
}

@end
